home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / AmiVoGL_MDEV.lha / src / mapping.c < prev    next >
C/C++ Source or Header  |  1994-04-12  |  2KB  |  109 lines

  1. #include "vogl.h"
  2.  
  3. static    float    Vcx, Vcy, Vsx, Vsy;
  4. static    Matrix    msave;
  5.  
  6. /*
  7.  * VtoWxy
  8.  *
  9.  * Return the world x and y values corresponding to the input
  10.  * screen x and y values.
  11.  */
  12. void VtoWxy(
  13.   float xs,
  14.   float ys,
  15.   float *xw,
  16.   float *yw)
  17. {
  18. float    d, a1, a2, b1, b2, c1, c2, A, B;
  19.  
  20. A = (xs - Vcx) / Vsx;
  21. B = (ys - Vcy) / Vsy;
  22.  
  23. a1 = msave[0][0] - msave[0][3] * A;
  24. a2 = msave[0][1] - msave[0][3] * B;
  25.  
  26. b1 = msave[1][0] - msave[1][3] * A;
  27. b2 = msave[1][1] - msave[1][3] * B;
  28.  
  29. c1 = msave[3][3] * A - msave[3][0];
  30. c2 = msave[3][3] * B - msave[3][1];
  31.  
  32. d = (a2 * b1 - b2 * a1);
  33.  
  34. if (d != 0.0) {
  35.     *xw = (b1 * c2 - c1 * b2) / d;
  36.     *yw = (c1 * a2 - a1 * c2) / d;
  37.     }
  38. else {
  39.     *xw = A;
  40.     *yw = B;
  41.     }
  42.  
  43. if (*xw == 0.0)
  44. *xw = A;
  45.  
  46. if (*yw == 0.0)
  47. *yw = B;
  48. }
  49.  
  50. /* ------------------------------------------------------------------------ */
  51.  
  52. /*
  53.  * _mapmsave
  54.  *
  55.  * saves the current transformation matrix as loaded by "loadmatrix" and
  56.  * before and other transformations (ie rotation and scaling) are
  57.  * concatenated with it.
  58.  */
  59. void _mapmsave(Matrix m)
  60. {
  61. copymatrix(msave, m);
  62. }
  63.  
  64. /* ------------------------------------------------------------------------ */
  65.  
  66. /*
  67.  * calcW2Vcoeffs
  68.  *
  69.  *    Calculate the linear coeffs defining the mapping of world
  70.  *    space to actual device space
  71.  */
  72. void CalcW2Vcoeffs(void)
  73. {
  74. Vcx = (float)(vdevice.maxVx + vdevice.minVx) * 0.5;
  75. Vcy = (float)(vdevice.maxVy + vdevice.minVy) * 0.5;
  76.  
  77. Vsx = (float)(vdevice.maxVx - vdevice.minVx) * 0.5;
  78. Vsy = (float)(vdevice.maxVy - vdevice.minVy) * 0.5;
  79. }
  80.  
  81. /* ------------------------------------------------------------------------ */
  82.  
  83. /*
  84.  * WtoVx
  85.  *
  86.  * return the Screen X coordinate corresponding to world point 'p' 
  87.  * (does the perspective division as well)
  88.  */
  89. int WtoVx(float p[])
  90. {
  91. return((int)(p[0] * Vsx / p[3] + Vcx + 0.5));
  92. }
  93.  
  94. /* ------------------------------------------------------------------------ */
  95.  
  96. /*
  97.  * WtoVy
  98.  *
  99.  * return the Screen Y coordinate corresponding to world point 'p' 
  100.  * (does the perspective division as well)
  101.  */
  102. int WtoVy(float p[])
  103. {
  104. return((int)(p[1] * Vsy / p[3] + Vcy + 0.5));
  105. }
  106.  
  107. /* ------------------------------------------------------------------------ */
  108.  
  109.